﻿var formTypeData=[{code:'0',name:'水费'},{code:'1',name:'电费'}];
var areaData=[{code:'0',name:'公摊'},{code:'1',name:'住宅'},{code:'2',name:'门面'},{code:'3',name:'其它'}];
var emptyData={total:0,rows:[]};
//初始化入口
$(function() {
    $('#dlg-addform').dialog('close');
    $('#dlg-changeformtype').dialog('close');
    $('#cbarea').combobox('loadData',areaData);
    $('#cbformType').combobox('loadData',formTypeData);
    
    $('#makeformlistGird').datagrid('options').onSelect=function(rowIndex, rowData){
        $('#formorderGrid').datagrid('options').url='../Controller.aspx?controller=MakeFormController&method=GetMakeOrderList&MeterHeadId='+rowData.MeterHeadId;
        $('#formorderGrid').datagrid('reload');
    };
    
    $('#formorderGrid').datagrid('options').onSelect=function(rowIndex, rowData){
        var rows= $('#formorderGrid').datagrid('getRows');
        for(var i=0;i<rows.length;i++){
            $('#formorderGrid').datagrid('endEdit', i);
        }
        $('#formorderGrid').datagrid('beginEdit', rowIndex);
        var editors =  $('#formorderGrid').datagrid('getEditors',rowIndex);
        var cell= $(editors[0].target).focus().select().bind("keydown",function(e){
            if(e.keyCode==13){                   
                $('#formorderGrid').datagrid('endEdit', rowIndex);
                 if(rows.length!=(parseInt(rowIndex)+1)){
                      $('#formorderGrid').datagrid('selectRow',parseInt(rowIndex)+1);
                 }
            }
        });
    };
    
    $('#formorderGrid').datagrid('options').onAfterEdit=function(rowIndex, rowData){
        var para={meterOrderId:rowData.MeterOrderId,thisNum:rowData.ThisNum};
	    requestAjax('../Controller.aspx?controller=MakeFormController&method=SaveThisNum',para,function(data){
	        $("#formorderGrid").datagrid("updateRow",{index:rowIndex,row:data}); 
	    });
    };
    
    webpagekey('formorderGrid');
    
});

function dlg_changeformtype_show(){
    $('#dlg-changeformtype').dialog('open');
}

function dlg_changeformtype_confirm(){
    
}

function btn_batchaddmakeform(){//批量创建表单
	$.messager.progress({
		title:'批量创建表单',
		msg:'正在创建中请等待...'
	});
	var para={meterType:$('#cbarea').combobox('getValue'),meterType2:$('#cbformType').combobox('getValue')};
	requestAjax('../Controller.aspx?controller=MakeFormController&method=BatchAddMakeForm',para,function(data){
	    btn_searchmakeform();
	    $.messager.progress('close');
	},function(){$.messager.progress('close');}
	);
}

function btn_addemptymakeform(){//创建空表单
    var para={meterType:$('#cbarea').combobox('getValue'),meterType2:$('#cbformType').combobox('getValue')};
	requestAjax('../Controller.aspx?controller=MakeFormController&method=AddEmptyMakeForm',para,function(data){
	    btn_searchmakeform();
	});
}

function btn_searchmakeform(){
    var meterType=$('#cbarea').combobox('getValue');
    var meterType2=$('#cbformType').combobox('getValue');
    $('#makeformlistGird').datagrid('options').url='../Controller.aspx?controller=MakeFormController&method=GetMakeformList&meterType='+meterType+'&meterType2='+meterType2;
    $('#makeformlistGird').datagrid('reload');
    
    $('#formorderGrid').datagrid('loadData',emptyData);
}

function btn_searchallmakeform(){
    var meterType=-1;
    var meterType2=-1;
    $('#makeformlistGird').datagrid('options').url='../Controller.aspx?controller=MakeFormController&method=GetMakeformList&meterType='+meterType+'&meterType2='+meterType2;
    $('#makeformlistGird').datagrid('reload');
    
    $('#formorderGrid').datagrid('loadData',emptyData);
}

function btn_deletemakeform(){
    var selected=$('#makeformlistGird').datagrid('getSelected');
    if(selected){
        $.messager.confirm('询问', '是否要删除此表单？', function(r){
				if (r){
					 requestAjax('../Controller.aspx?controller=MakeFormController&method=DeleteMakeform', { makeheadId: selected.MeterHeadId }, function(data) {
                            $('#makeformlistGird').datagrid('reload');
                            $('#formorderGrid').datagrid('loadData',emptyData);
                        });
				}
			});
    }
}

function typeFormatter1(value){
   
	for(var i=0; i<areaData.length; i++){
		if (areaData[i].code == value) return areaData[i].name;
	}
	return value;
}

function typeFormatter2(value){
    for(var i=0; i<formTypeData.length; i++){
		if (formTypeData[i].code == value) return formTypeData[i].name;
	}
	return value;
}

function thisnumStyler(value){
    return 'background-color:#efefef;color:blue;';
}

function btn_makeformorder_add(){
    var selected=$('#makeformlistGird').datagrid('getSelected');
    if(selected){
        var meterType=selected.MeterType;
        var meterType2=selected.MeterType2;
        $('#meterlistGrid').datagrid('options').url='../Controller.aspx?controller=MakeFormController&method=GetMakerList&meterType='+meterType+'&meterType2='+meterType2;
        $('#meterlistGrid').datagrid('reload');
        $('#dlg-addform').dialog('open');
        $('#searchmeter').searchbox('setValue','');
    }
}

function btn_makeformorder_reload(){
    $("#formorderGrid").datagrid('reload'); 
}

function btn_makeformorder_del(){
    var selected=$('#formorderGrid').datagrid('getSelected');
    if(selected){
        $.messager.confirm('询问', '是否要删除此记录？', function(r){
				if (r){
					 requestAjax('../Controller.aspx?controller=MakeFormController&method=DeleteMakeOrder', { MeterOrderId: selected.MeterOrderId }, function(data) {
                            $('#formorderGrid').datagrid('reload');
                        });
				}
			});
    }
}

function btn_materformorder_auditing(){
    var selected=$('#makeformlistGird').datagrid('getSelected');
    if(selected){
     var rows=$('#formorderGrid').datagrid('getRows');
     if(rows.length>0){
        var meterHeadId= selected.MeterHeadId;
        requestAjax('../Controller.aspx?controller=MakeFormController&method=AuditingMeterfrom', { MeterHeadId: meterHeadId }, function(data) {
                                $('#makeformlistGird').datagrid('reload');
                                $('#formorderGrid').datagrid('loadData',emptyData);
                            });
        }
    }
}

function dlg_meterlist_search(value,name){
    var selected=$('#makeformlistGird').datagrid('getSelected');
    if(selected){
        var meterType=selected.MeterType;
        var meterType2=selected.MeterType2;
                        
        $('#meterlistGrid').datagrid('options').url='../Controller.aspx?controller=MakeFormController&method=SearchMakerList&meterType='+meterType+'&meterType2='+meterType2+'&searchKey='+value;
        $('#meterlistGrid').datagrid('reload');
    }
}

function dlg_addform_confirm(){
    var selected=$('#makeformlistGird').datagrid('getSelected');
    if(selected){
        var rows = $('#meterlistGrid').datagrid('getSelections');
        if(rows.length>0){
            var ids = [];			 
			for(var i=0;i<rows.length;i++){
				ids.push(rows[i].MeterId);
			}
            requestAjax('../Controller.aspx?controller=MakeFormController&method=AddMakerOrder', { MeterHeadId: selected.MeterHeadId,MeterIds:ids.join(',') }, function(data) {
                                dlg_addform_close();
                                $('#formorderGrid').datagrid('reload');
                            });
        }
    }
}

function dlg_addform_close(){
    $('#dlg-addform').dialog('close');
}